﻿using SpaceNeedle.City6473.Web.Data.Models;
using SpaceNeedle.SDK.Web.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpaceNeedle.City6473.Web.Data.Repositories
{
    public class UsersAuthenticationRepository : EntitiyFrameworkRepository<UsersAuthentication>
    {
        public UsersAuthenticationRepository() : base(new City6473Entities())
        {
            this.dataContext.Configuration.ProxyCreationEnabled = false;
        }

        public override UsersAuthentication GetById(Guid id)
        {
            return this.GetAll().Where(x => x.Id == id).SingleOrDefault();
        }

        public override UsersAuthentication GetById(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return null;
            }

            return this.GetById(new Guid(id));
        }

        public UsersAuthentication Authenticate(string login, string password)
        {
            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
            {
                return null;
            }

            try
            {
                password = Security.Encode(password);

                return this.GetAll().Where(w => w.Email == login && w.Password == password).SingleOrDefault();
            }
            catch { }

            return null;
        }
    }
}
